/*
 * Project Name: cms_web
 * File Name: ClassesAction.java
 * Copyright: ZKTeco Corporation 2015
 * Description: TODO 请在此处添加对本文件的描述
 * Creator: Administrator
 * Create Date: 2015年3月16日 下午3:38:34
 */
package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.ClassesService;

@Controller
@RequestMapping("/admin/classes")
public class ClassesAction extends AdminBaseAction
{
	@Autowired
	private ClassesService classesService;
	@InitBinder("classesEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("classesEntity.");
	}
	
	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String add(ModelMap modelMap)
	{
		return "system/classes/add";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/addNew.json", method = RequestMethod.POST)
	public JsonVo<String> addNewClasses(@ModelAttribute ClassesEntity classesEntity) throws Exception
	{
		JsonVo<String> json = new JsonVo<String>();
		//验证填写的是否正确
		if (classesEntity.getName().equals("")) {
			json.getErrors().put("name", this.getMsgByMsgKey("system.classesNameNotEmpty"));
		}
		if (classesEntity.getAliasName().equals("")) {
			json.getErrors().put("aliasName", this.getMsgByMsgKey("system.classesAliasNotEmpty"));
		}
		if (classesEntity.getType().equals("")) {
			json.getErrors().put("type", this.getMsgByMsgKey("system.classesTypeNotEmpty"));
		}
		if (classesService.isExistByAliasName(classesEntity.getAliasName())==true) {
			json.getErrors().put("aliasName", this.getMsgByMsgKey("system.classesAliasHasExist"));
		}
		
		if (validateJsonIsError(json)) return json;
		classesService.insert(classesEntity);
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("pageVo", classesService.getAllListPage(pageNum));
		return "system/classes/all";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/getClassesByType.json", method = RequestMethod.POST)
	public JsonVo<List<ClassesEntity>> getClassesByType(@RequestParam(value = "fatherId", defaultValue = "0") int fatherId,@RequestParam(value = "type") String type) throws Exception
	{
		JsonVo<List<ClassesEntity>> json = new JsonVo<List<ClassesEntity>>();
		ClassesEntity classesEntity=new ClassesEntity();
		classesEntity.setType(type);
		classesEntity.setFatherId(fatherId);
		List<ClassesEntity> list=classesService.getClassesByType(classesEntity);
		json.setT(list);
		json.setResult(true);
		return json;
	}
	
	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "id", defaultValue = "0") long id,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		ClassesEntity classesEntity=classesService.getById(id);
		modelMap.put("classesEntity", classesEntity);
		
		return "system/classes/update";
	}

	/**
	 * 
	 */
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> deleteClasses(
			@RequestParam(value = "id") long id) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		classesService.deleteById(id);
		json.setResult(true);
		return json;
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> editClasses(@ModelAttribute ClassesEntity classesEntity)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		ClassesEntity classesEntityOld = classesService.getById(classesEntity.getId());
		classesEntityOld.setAliasName(classesEntity.getAliasName());
		classesEntityOld.setName(classesEntity.getName());
		classesEntityOld.setFatherId(classesEntity.getFatherId());
		classesEntityOld.setLastUpdateDate(new Date());
		classesEntityOld.setType(classesEntity.getType());
		classesEntityOld.setOrderNum(classesEntity.getOrderNum());
		classesEntityOld.setIsHeader(classesEntity.getIsHeader());
		classesEntityOld.setIsShowOnIndex(classesEntity.getIsShowOnIndex());
		classesEntityOld.setIsShowRollOnIndexPage(classesEntity.getIsShowRollOnIndexPage());
		classesService.update(classesEntityOld);
		json.setResult(true);
		return json;
	}
	
}
